package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.context.BaseContext;
import com.mfl.mapper.ExamDetailMapper;
import com.mfl.mapper.ExamMapper;
import com.mfl.mapper.TestExamTopicMapper;
import com.mfl.mapper.UserMapper;
import com.mfl.pojo.dto.ExamDTO;
import com.mfl.pojo.dto.ExamPageQueryDTO;
import com.mfl.pojo.entity.Exam;
import com.mfl.pojo.entity.ExamDetail;
import com.mfl.pojo.entity.TestExamTopic;
import com.mfl.pojo.entity.User;
import com.mfl.pojo.vo.ExamPageQueryVO;
import com.mfl.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamDetailMapper examDetailMapper;
    @Autowired
    private TestExamTopicMapper testExamTopicMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 添加考试记录
     * @param examDTO
     */
    @Transactional
    @Override
    public void insert(ExamDTO examDTO) {
        // 向考试表中添加记录
        // 从考试题目信息中计算出所的总分
        AtomicInteger totalScore = new AtomicInteger();
        examDTO.getTestExamTopicList().forEach(testExamTopic -> {
            // 根据试卷id和题目id查询题目信息
            TestExamTopic topic = testExamTopicMapper.selectById(testExamTopic.getId(), testExamTopic.getTestExamId());
            if(testExamTopic.getTopicUserOption().equals(topic.getTopicRightOption())){
                totalScore.addAndGet(topic.getTopicScore());
            }
        });
        // 查询用户的姓名
        User user = userMapper.getById(BaseContext.getCurrentId());
        Exam exam = Exam.builder()
                .userId(BaseContext.getCurrentId())
                .userName(user.getName())
                .testCategoryId(examDTO.getTestCategoryId())
                .testExamName(examDTO.getTestExamName())
                .totalScore(totalScore.get())
                .spentTime(examDTO.getSpentTime())
                .createTime(LocalDateTime.now())
                .build();
        examMapper.insert(exam);
        // 向考试记录详情表中添加记录
        examDTO.getTestExamTopicList().forEach(testExamTopic -> {
            // 根据试卷id和题目id查询题目信息
            TestExamTopic topic = testExamTopicMapper.selectById(testExamTopic.getId(), testExamTopic.getTestExamId());

            ExamDetail examDetail = ExamDetail.builder()
                    .examId(exam.getId())
                    .examTopic(testExamTopic.getTopicName())
                    .testExamId(testExamTopic.getTestExamId())
                    .topicScore(topic.getTopicScore())
                    .topicRightOption(topic.getTopicRightOption())
                    .topicUserOption(testExamTopic.getTopicUserOption())
                    .status(testExamTopic.getTopicUserOption().equals(topic.getTopicRightOption()) ? 1 : 0)
                    .build();

            examDetailMapper.insert(examDetail);
        });
    }

    /**
     * 分页查询考试记录
     * @param examPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(ExamPageQueryDTO examPageQueryDTO) {
        PageHelper.startPage(examPageQueryDTO.getPage(), examPageQueryDTO.getPageSize());
        Page<ExamPageQueryVO> page = examMapper.pageQuery(examPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除考试记录
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        examMapper.deleteByIds(ids);
        // 删除考试详情表中对应的题目
        examDetailMapper.deleteByExamIds(ids);
    }

    /**
     * 分页查询用户考试记录
     * @param examPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQueryUserExamRecord(ExamPageQueryDTO examPageQueryDTO) {
        examPageQueryDTO.setUserId(BaseContext.getCurrentId());
        PageHelper.startPage(examPageQueryDTO.getPage(), examPageQueryDTO.getPageSize());
        Page<ExamPageQueryVO> page = examMapper.pageQuery(examPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }
}
